package com.clerkq.FP
{
	import com.clerkq.FP.model.PageInfo;
	import com.library.utils.Track;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;

	public class Page extends Sprite
	{
		//======================================================================
		//						Variables
		//======================================================================
		private var _index:int;
		private var _depth:int;
		private var _side:int; // -1 is LeftSide && +1 is RightSide
		private var _position:Object;
		private var _state:String;
		
		private var _contentInfo:PageInfo;
		
		//======================================================================
		//						Elements
		//======================================================================
		private var _page:Sprite;
		private var _pageMask:Sprite;
		private var _face:Sprite;
		private var _contentMask:Sprite;
		private var _content:MovieClip;
		
		private var _shade:Sprite;
		private var _shadow:Sprite;
		private var _shadowMask:Sprite;	
		
		private var _contentLoader:Loader;
		
		
		public function Page(index:int, depth:int){
			
			_contentLoader = new Loader();
			_contentLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onContentLoaded);
			_contentLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);
			
			_index = index;
			_depth = depth;

			_side = ((index % 2) == 0) ? -1 : 1;//偶数是-1,奇数是1
			create();
			
		}
		
		public function load():void{
			if(_contentInfo != null){
				_contentLoader.load(new URLRequest(_contentInfo.itemPath));
			}
		}
		
		private function create():void{
			
			_page = new Sprite();
			_pageMask = new Sprite();			
			addChild(_page);
			addChild(_pageMask);
			_page.mask = _pageMask;
			
			_face = new Sprite();

			_contentMask = new Sprite();
			_page.addChild(_face);
			_page.addChild(_contentMask);
			_face.mask = _contentMask;			
		}
		
		// Events
		private function onContentLoaded(evt:Event):void{
			_content.addChild(_contentLoader);
		}
		private function onLoadError(evt:IOErrorEvent):void{
			var pathSwf:String = _contentInfo.itemPath.substr(0, _contentInfo.itemPath.length - 4) + ".swf";
			_contentLoader.load(new URLRequest(pathSwf));
		}
		
		// Public //////////////////////
		public function dispose():void{
		}
		
		//======================================================================
		//						Get And Set
		//======================================================================
		public function get contentInfo():PageInfo		{	return _contentInfo;	}
		public function get index():int					{	return _index;			}
		public function get depth():int					{	return _depth;			}
		public function get side():int					{	return _side;			}
		public function get position():Object			{	return _position;		}		
		public function get face():Sprite				{	return _face;			}
		public function get contentMask():Sprite		{	return _contentMask;	}
		public function get page():Sprite				{	return _page;			}
		public function get pageMask():Sprite			{	return _pageMask;		}
		public function get state():String				{	return _state;			}
		public function get content():MovieClip			{	return _content;		}
		public function get shade():Sprite				{	return _shade;			}
		public function get shadow():Sprite				{	return _shadow;			}
		public function get shadowMask():Sprite			{	return _shadowMask;		}
		
		public function set contentInfo(value:PageInfo):void		{	_contentInfo = value;	}
		public function set index(value:int):void					{	_index = value;			}
		public function set depth(value:int):void					{	_depth = value;			}
		public function set side(value:int):void					{	_side = value;			}
		public function set position(value:Object):void				{	_position = value;		}		
		public function set face(value:Sprite):void					{	_face = value;			}
		public function set contentMask(value:Sprite):void			{	_contentMask = value;	}
		public function set page(value:Sprite):void					{	_page = value;			}
		public function set pageMask(value:Sprite):void				{	_pageMask = value;		}
		public function set state(value:String):void				{	_state = value;			}
		public function set content(value:MovieClip):void			{	_content = value;		}
		public function set shade(value:Sprite):void				{	_shade = value;			}
		public function set shadow(value:Sprite):void				{	_shadow = value;		}
		public function set shadowMask(value:Sprite):void			{	_shadowMask = value;	}
	}
}